package com.chase.passwordmaster.search

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.PagerAdapter
import com.chase.passwordmaster.R
import com.chase.passwordmaster.entity.DecryptedAccount

class SearchPagerAdapter(
    val context: Context,
    val searchResultList: List<List<DecryptedAccount>>,
    val titles: List<String>,
    val initItem: ((view: View, position: Int) -> Unit)? = null
) : PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view = View.inflate(context, R.layout.item_search_page, null)

         initItem?.let { it(view, position) }

        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object`

    override fun getCount(): Int = searchResultList.size

    override fun getPageTitle(position: Int): CharSequence? = titles[position]

    // 以便 notifyDataSetChanged() 能够刷新，但是效率不高，会全部刷新
    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }
}